package org.eae.eldf.service.sys.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Sets;

import org.apache.commons.lang.StringUtils;
import org.eae.eldf.dao.sys.ISysOperatorDao;
import org.eae.eldf.dao.sys.impl.SysOperatorDaoImpl;
import org.eae.eldf.dto.sys.OperatorRoleCheckDto;
import org.eae.eldf.dto.sys.SysOperatorQueryDto;
import org.eae.eldf.entity.sys.SysMenu;
import org.eae.eldf.entity.sys.SysOperator;
import org.eae.eldf.entity.sys.SysOperatorRole;
import org.eae.eldf.entity.sys.SysPurview;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.framework.service.BaseServiceImpl;
import org.eae.eldf.framework.spring.SpringSecurityUtils;
import org.eae.eldf.service.sys.SysOperatorService;
import org.eae.eldf.webapp.vo.TreeVo;
@Service
@Transactional
public class SysOperatorServiceImpl extends BaseServiceImpl<SysOperator> implements
		SysOperatorService {

	private ISysOperatorDao operatorDao;
	

	@Autowired
	public void setOperatorDao(SysOperatorDaoImpl operatorDao) {
		this.operatorDao = operatorDao;
		this.dao = operatorDao;
	}

	@Override
	public SysOperator queryByUser(String loginUsername) {
		return operatorDao.findOperatorByLoginName(loginUsername);
	}

	@Override
	public Pagination<SysOperator> queryPageUserList(Pagination<SysOperator> pager,SysOperatorQueryDto dto) {
		pager = operatorDao.pageUserList(pager, dto);
		return pager;
	}

	@Override
	public void delete(Long id) {
		Criteria criteria = Criteria.create(SysOperator.class).set("bisdelete", 1).where("sysOperatorId", new Object[]{id});
		operatorDao.update(criteria);
	}

	@Override
	public SysOperator queryById(Long id) {
		return operatorDao.get(SysOperator.class, id);
	}

	@Override
	public List<OperatorRoleCheckDto> queryRoleByUser(Long operatorId) {
		return operatorDao.findRoleByUser(operatorId);
	}

	@Override
	public void saveOperatorRole(Criteria c,Long operatorId, String role_ids) {
		operatorDao.update(c);
		//先删除用户的角色
		operatorDao.delete(Criteria.create(SysOperatorRole.class).where("sysOperatorId", new Object[]{operatorId}));
		if(StringUtils.isNotBlank(role_ids)){
			String[] si = role_ids.split(",");
			for(String roleId : si){
				Criteria criteria = Criteria.create(SysOperatorRole.class).set("dcreatetime", new Date())
						.set("dlastupdatetime", new Date())
						.set("slastupdateuser", SpringSecurityUtils.getCurrentUserName())
						.set("sysOperatorId", operatorId)
						.set("sysRoleId", Long.parseLong(roleId));
				operatorDao.insert(criteria);
			}
		}
		
		
	}

	@Override
	public HashSet<Long> getOperatorMenus(String loginName) {
		HashSet<Long> authSet = Sets.newHashSet();
		List<SysPurview> list = operatorDao.findUserAuthCodes(loginName);
		for(SysPurview p : list){
			authSet.add(p.getParentid());
		}
		return authSet;
	}

	@Override
	public List<TreeVo> getMenusByUser() {
		List<SysMenu> listMenu = operatorDao.queryList(Criteria.create(SysMenu.class));
		
		HashSet<Long> perSet = this.getOperatorMenus(SpringSecurityUtils.getCurrentUserName());
		
		Map<String, TreeVo> treeMap = new LinkedHashMap<String, TreeVo>();
		
		TreeVo root = new TreeVo();
		for (SysMenu menu : listMenu) {
			if(menu.getBisleaf()==0 || perSet.contains(menu.getSysMenuId())){
				TreeVo tree = new TreeVo();
				tree.setId(""+menu.getSysMenuId());
				tree.setText(menu.getSname());
				tree.setLeaf(menu.getBisleaf()==0?false:true);
				tree.setIcon(StringUtils.isBlank(menu.getSiconcls())?"":menu.getSiconcls());
				tree.setParentId(menu.getParentid()!=null?""+menu.getParentid():null);
				if (menu.getBisleaf() == 1) {
					tree.setUrl(menu.getSmenupath());
				}
				treeMap.put(""+menu.getSysMenuId(), tree);
			}
		}
		Iterator<Map.Entry<String, TreeVo>> it = treeMap.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, TreeVo> entry = it.next();
			TreeVo tree = entry.getValue();
			if (StringUtils.isBlank(tree.getParentId())) {
				root.getMenus().add(tree);
			}else{
				TreeVo parent = treeMap.get(tree.getParentId());// 根据父Id查询
				if (parent != null) {
					parent.getMenus().add(tree);
				}
			}
		}
		root.removeNoRight();
		return root.getMenus();
	}

	@Override
	public void insertUserAndRole(SysOperator operator, String roleIds) {
		Long userid = operatorDao.insert(operator);
		if(StringUtils.isNotBlank(roleIds)){
			String[] roleIdArray = roleIds.split(",");
			for(String r : roleIdArray){
				Long roleId = Long.parseLong(r);
				SysOperatorRole sor = new SysOperatorRole();
				sor.setSysOperatorId(userid);
				sor.setSysRoleId(roleId);
				operatorDao.insert(sor);
			}
		}
	}
	
	

}
